package com.example.util;

import com.example.sikong.service.RedisProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

@Component
public class CreateLiveUrl {
    private static String pushDomain;

    private static String pushKey;
    private static Long pushExpireTime;
    private static String pullDomain;
    private static String pullKey;
    private static Long pullExpireTime;
    private static String pushAppName;
    private static String pullAppName;

    @Autowired
    private RedisProvider redisProvider;


    @Value("${aliyun.live.push-host}")
    public void setPushDomain(String pushDomain) {
        CreateLiveUrl.pushDomain = pushDomain;
    }

    @Value("${aliyun.live.pull-host}")
    public void setPullDomain(String pullDomain) {
        CreateLiveUrl.pullDomain = pullDomain;
    }


    @Value("${aliyun.live.push-key}")
    public void setPushKey(String pushDomain) {
        CreateLiveUrl.pushKey = pushDomain;
    }

    @Value("${aliyun.live.pull-key}")
    public void setPullKey(String pushDomain) {
        CreateLiveUrl.pullKey = pushDomain;
    }

    @Value("${aliyun.live.push-expire-time}")
    public void setPushExpireTime(long pushExpireTime) {
        CreateLiveUrl.pushExpireTime = pushExpireTime;
    }

    @Value("${aliyun.live.pull-expire-time}")
    public void setPullExpireTime(long pullExpireTime) {
        CreateLiveUrl.pullExpireTime = pullExpireTime;
    }

    @Value("${aliyun.live.push-app-name}")
    public void setPushAppName(String pushAppName) {
        CreateLiveUrl.pushAppName = pushAppName;
    }

    @Value("${aliyun.live.pull-app-name}")
    public void setPullAppName(String pullAppName) {
        CreateLiveUrl.pullAppName = pullAppName;
    }

    /**
     * 计算md5
     *
     * @param param
     * @return
     */
    public static String md5(String param) {
        if (param == null || param.length() == 0) {
            return null;
        }
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(param.getBytes());
            byte[] byteArray = md5.digest();

            BigInteger bigInt = new BigInteger(1, byteArray);
            // 参数16表示16进制
            String result = bigInt.toString(16);
            // 不足32位高位补零
            while (result.length() < 32) {
                result = "0" + result;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String generate_push_url(String streamName) {
        return generate_push_url(pushDomain, pushKey, pushAppName, streamName, pushExpireTime);
    }

    public static Map<String, String> general_pull_url(String streamName) {
        return general_pull_url(pullDomain, pullKey, pullAppName, streamName, pullExpireTime);
    }


    /**
     * 生成推流地址
     *
     * @param pushDomain 推流域名
     * @param pushKey    推流域名配置的鉴权Key
     * @param appName    推流AppName
     * @param streamName 推流StreamName
     * @param expireTime 过期时间（单位是秒）
     */
    public static String generate_push_url(String pushDomain, String pushKey, String appName, String streamName, long expireTime) {
        String pushUrl = "";
        //推流域名未开启鉴权功能的情况下
        if (pushKey == "") {
            pushUrl = "rtmp://" + pushDomain + "/" + appName + "/" + streamName;
        } else {
            long timeStamp = System.currentTimeMillis() / 1000L + expireTime;
            String stringToMd5 = "/" + appName + "/" + streamName + "-" + timeStamp + "-0-0-" + pushKey;
            String authKey = md5(stringToMd5);
            pushUrl = "rtmp://" + pushDomain + "/" + appName + "/" + streamName + "?auth_key=" + timeStamp + "-0-0-" + authKey;
        }
        return pushUrl;
    }

    /**
     * 生成播放地址
     *
     * @param pullDomain 播放域名
     * @param pullKey    播放鉴权Key
     * @param appName    播放appName（同推流appName)
     * @param streamName 播放streamName 同推流streamName）
     * @param expireTime 过期时间（单位是秒
     */
    public static Map<String, String> general_pull_url(String pullDomain, String pullKey, String appName, String streamName, long expireTime) {
        String rtmpUrl = ""; //rtmp的拉流地址
        String hlsUrl = "";  //m3u8的拉流地址
        String flvUrl = "";  //flv的拉流地址
        String artcUrl = "";  // artc
        //播放域名未配置鉴权Key的情况下
        if (pullKey == "") {
            rtmpUrl = "rtmp://" + pullDomain + "/" + appName + "/" + streamName;
            hlsUrl = "https://" + pullDomain + "/" + appName + "/" + streamName + ".m3u8";
            flvUrl = "https://" + pullDomain + "/" + appName + "/" + streamName + ".flv";
            artcUrl = "artc://" + pullDomain + "/" + appName + "/" + streamName;
        } else {
            long timeStamp = System.currentTimeMillis() / 1000L + expireTime;
            String rtmpToMd5 = "/" + appName + "/" + streamName + "-" + Long.toString(timeStamp) + "-0-0-" + pullKey;
            String rtmpAuthKey = md5(rtmpToMd5);
            rtmpUrl = "rtmp://" + pullDomain + "/" + appName + "/" + streamName + "?auth_key=" + Long.toString(timeStamp) + "-0-0-" + rtmpAuthKey;

            String hlsToMd5 = "/" + appName + "/" + streamName + ".m3u8-" + Long.toString(timeStamp) + "-0-0-" + pullKey;
            String hlsAuthKey = md5(hlsToMd5);
            hlsUrl = "https://" + pullDomain + "/" + appName + "/" + streamName + ".m3u8" + "?auth_key=" + Long.toString(timeStamp) + "-0-0-" + hlsAuthKey;

            String flvToMd5 = "/" + appName + "/" + streamName + ".flv-" + Long.toString(timeStamp) + "-0-0-" + pullKey;
            String flvAuthKey = md5(flvToMd5);
            flvUrl = "https://" + pullDomain + "/" + appName + "/" + streamName + ".flv" + "?auth_key=" + Long.toString(timeStamp) + "-0-0-" + flvAuthKey;

            String artcToMd5 = "/" + appName + "/" + streamName + "-" + Long.toString(timeStamp) + "-0-0-" + pullKey;
            String artcAuthKey = md5(artcToMd5);
            artcUrl = "artc://" + pullDomain + "/" + appName + "/" + streamName + "?auth_key=" + Long.toString(timeStamp) + "-0-0-" + artcAuthKey;
        }
        Map<String, String> urlMap = new HashMap<>();
        urlMap.put("rtmp", rtmpUrl);
        urlMap.put("hls", hlsUrl);
        urlMap.put("flv", flvUrl);
        urlMap.put("artc", artcUrl);
        return urlMap;
    }
}
